#!/usr/bin/env node

/**
 * 简单的语法验证脚本
 * 检查TypeScript文件的基本语法结构
 */

const fs = require('fs');
const path = require('path');

// 要检查的文件列表
const filesToCheck = [
  'src/main/ets/components/plugin/SuotaLib/suota/SuotaManager.ets',
  'src/main/ets/components/plugin/SuotaLib/global/SuotaProfile.ets',
  'src/main/ets/components/plugin/SuotaLib/suota/SuotaProtocol.ets',
  'src/main/ets/components/plugin/SuotaLib/SuotaBluetoothManager.ets',
  'src/main/ets/components/plugin/SuotaLib/suota/SuotaFile.ets',
  'src/main/ets/components/plugin/SuotaLib/suota/HeaderInfoBuilder.ets',
  'src/main/ets/components/plugin/SuotaLib/suota/MemoryDeviceOperation.ets',
  'src/test/SuotaPluginTest.ets',
  'src/test/SuotaIntegrationTest.ets'
];

console.log('🔍 开始验证HarmonyOS SUOTA插件语法...\n');

let allValid = true;

filesToCheck.forEach(filePath => {
  console.log(`📄 检查文件: ${filePath}`);
  
  try {
    if (!fs.existsSync(filePath)) {
      console.log(`   ❌ 文件不存在`);
      allValid = false;
      return;
    }
    
    const content = fs.readFileSync(filePath, 'utf8');
    
    // 基本语法检查
    const isTestFile = filePath.includes('/test/');
    const isProfileFile = filePath.includes('Profile.ets');
    
    const checks = [
      {
        name: '导入语句',
        pattern: /import\s+.*from\s+['"][^'"]+['"];?/,
        required: !isProfileFile // Profile文件可能只有常量定义，不需要导入
      },
      {
        name: '类定义',
        pattern: isTestFile ? 
          /export\s+default\s+function\s+\w+/ : // 测试文件使用函数导出
          /export\s+(default\s+)?class\s+\w+/, // 普通文件使用类导出
        required: true
      },
      {
        name: '方法定义',
        pattern: /\w+\s*\([^)]*\)\s*:\s*\w+\s*{/,
        required: false
      },
      {
        name: '括号匹配',
        check: () => {
          const openBraces = (content.match(/{/g) || []).length;
          const closeBraces = (content.match(/}/g) || []).length;
          return openBraces === closeBraces;
        }
      }
    ];
    
    let fileValid = true;
    
    checks.forEach(check => {
      if (check.pattern) {
        const found = check.pattern.test(content);
        if (check.required && !found) {
          console.log(`   ❌ 缺少${check.name}`);
          fileValid = false;
        } else if (found) {
          console.log(`   ✅ ${check.name}正确`);
        }
      } else if (check.check) {
        const result = check.check();
        if (result) {
          console.log(`   ✅ ${check.name}正确`);
        } else {
          console.log(`   ❌ ${check.name}不匹配`);
          fileValid = false;
        }
      }
    });
    
    // 检查文件大小
    const stats = fs.statSync(filePath);
    const fileSizeKB = Math.round(stats.size / 1024);
    console.log(`   📊 文件大小: ${fileSizeKB}KB`);
    
    if (fileValid) {
      console.log(`   ✅ 文件语法验证通过\n`);
    } else {
      console.log(`   ❌ 文件语法验证失败\n`);
      allValid = false;
    }
    
  } catch (error) {
    console.log(`   ❌ 读取文件失败: ${error.message}\n`);
    allValid = false;
  }
});

// 检查配置文件
console.log('📋 检查配置文件...');

const configFiles = [
  { path: 'oh-package.json5', name: '项目配置' },
  { path: 'src/main/module.json5', name: '模块配置' },
  { path: 'src/main/resources/base/element/string.json', name: '字符串资源' }
];

configFiles.forEach(config => {
  if (fs.existsSync(config.path)) {
    console.log(`   ✅ ${config.name}存在`);
  } else {
    console.log(`   ❌ ${config.name}缺失`);
    allValid = false;
  }
});

console.log('\n' + '='.repeat(50));

if (allValid) {
  console.log('🎉 所有文件验证通过！HarmonyOS SUOTA插件已准备就绪。');
  console.log('\n📝 下一步建议:');
  console.log('1. 在HarmonyOS DevEco Studio中打开项目');
  console.log('2. 运行单元测试验证功能');
  console.log('3. 在真实设备上测试蓝牙功能');
  console.log('4. 集成到Flutter应用中进行端到端测试');
} else {
  console.log('❌ 发现问题，请检查上述错误并修复。');
  process.exit(1);
}

console.log('\n🔧 技术栈总结:');
console.log('- 平台: HarmonyOS NEXT (API 12+)');
console.log('- 语言: TypeScript/ArkTS');
console.log('- 蓝牙: @ohos.bluetooth.ble');
console.log('- 协议: Dialog Semiconductor SUOTA');
console.log('- 测试: @ohos/hypium');